package com.awesome.videodemo.video.exo;

import android.content.Context;

import com.awesome.common.util.log.ShowLogUtil;
import com.awesome.videodemo.video.AbsPlayer;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.video.VideoListener;

/**
 * Author: zhangjingfang
 * Email:zhangjingfang@jeejio.com
 * Date: 2022/7/21
 * Description:类描述
 */
public class ExoPlayer extends AbsPlayer {
    private String TAG = getClass().getSimpleName();
    private SimpleExoPlayer mSimpleExoPlayer;

    public ExoPlayer(Context context) {
        mSimpleExoPlayer = new SimpleExoPlayer.Builder(context).build();
        mSimpleExoPlayer.addListener(new Player.EventListener() {
            @Override
            public void onIsPlayingChanged(boolean isPlaying) {
                ShowLogUtil.info(TAG, "onIsPlayingChanged" + " : " + "isPlaying--->" + isPlaying);
            }

            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                ShowLogUtil.info(TAG, "onPlayerStateChanged" + " : " + "playWhenReady--->" + playWhenReady + ",playbackState--->" + playbackState);
                if (!playWhenReady && playbackState == Player.STATE_BUFFERING) {
                    if (mMediaPlayerListener != null)
                        mMediaPlayerListener.onPrepared();
                }
                if (playWhenReady && playbackState == Player.STATE_ENDED) {
                    if (mMediaPlayerListener != null)
                        mMediaPlayerListener.onComplete();
                }
            }

            @Override
            public void onPlayerError(ExoPlaybackException error) {
                ShowLogUtil.info(TAG, "onPlayerError" + " : " + "error--->" + error);
                if (mMediaPlayerListener != null)
                    mMediaPlayerListener.onError();
            }
        });
        mSimpleExoPlayer.addVideoListener(new VideoListener() {
            @Override
            public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) {
                if (mMediaPlayerListener != null)
                    mMediaPlayerListener.onVideoSizeChanged(width, height);
            }
        });
    }

    @Override
    public void start() {
        if (mSimpleExoPlayer.getPlaybackState() == Player.STATE_ENDED){
            mSimpleExoPlayer.seekToDefaultPosition();
        }
        mSimpleExoPlayer.setPlayWhenReady(true);
    }

    @Override
    public void pause() {
        mSimpleExoPlayer.setPlayWhenReady(false);
    }

    public void stop() {
        mSimpleExoPlayer.stop(true);
    }

    @Override
    public void seekTo(long position) {
        mSimpleExoPlayer.seekTo(position);
    }

    @Override
    public boolean isPlaying() {
        return mSimpleExoPlayer.isPlaying();
    }

    @Override
    public long getCurrentPosition() {
        return mSimpleExoPlayer.getCurrentPosition();
    }

    @Override
    public long getDuration() {
        return mSimpleExoPlayer.getDuration();
    }

    public SimpleExoPlayer getSimpleExoPlayer() {
        return mSimpleExoPlayer;
    }

}
